<template>
	<view class="container">
		<view class="form-container">
			<text class="title">注册</text>

			<view class="input-group">
				<input v-model="registerForm.username" class="input" placeholder="请输入用户名(2-20位)" />
				<input v-model="registerForm.password" class="input" password placeholder="请输入密码(至少6位)" />
				<input v-model="confirmPassword" class="input" password placeholder="请确认密码" />
			</view>

			<button class="btn-primary" :disabled="loading" @tap="handleRegister">
				{{ loading ? '注册中...' : '注册' }}
			</button>

			<view class="link-text">
				<text>已有账号？</text>
				<text class="link" @tap="goToLogin">去登录</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				confirmPassword: '', // 修复：将confirmPassword移到顶层data
				loading: false, // 新增：加载状态
				registerForm: {
					username: '',
					password: ''
				}
			}
		},
		methods: {
			async handleRegister() {
				// 基础验证
				console.log("handleRegister-->");
				if (!this.registerForm.username || !this.registerForm.password) {
					uni.showToast({
						title: '请输入用户名和密码',
						icon: 'none'
					})
					return
				}

				// 用户名长度验证
				if (this.registerForm.username.length < 2 || this.registerForm.username.length > 20) {
					uni.showToast({
						title: '用户名长度必须在2-20位之间',
						icon: 'none'
					})
					return
				}

				// 密码强度验证
				if (this.registerForm.password.length < 6) {
					uni.showToast({
						title: '密码长度不能少于6位',
						icon: 'none'
					})
					return
				}

				// 修复：使用正确的confirmPassword引用
				if (this.registerForm.password !== this.confirmPassword) {
					uni.showToast({
						title: '两次密码不一致',
						icon: 'none'
					})
					return
				}

				// 防止重复提交
				if (this.loading) return;

				this.loading = true;
				uni.showLoading({
					title: '注册中...'
				})

				try {
					const result = await uniCloud.callFunction({
						name: 'addUser',
						data: this.registerForm,
					})

					uni.hideLoading();
					this.loading = false;

					if (result.result.success) {
						uni.showToast({
							title: '注册成功',
							icon: 'success',
							duration: 2000
						});

						// 注册成功后延迟跳转
						setTimeout(() => {
							uni.redirectTo({
								url: '/pages/login/login'
							})
						}, 1500)
					} else {
						// 显示云函数返回的具体错误信息
						const errorMsg = result.result.errorMessage || '注册失败';
						uni.showToast({
							title: errorMsg,
							icon: 'none',
							duration: 3000
						})
					}
				} catch (error) {
					uni.hideLoading();
					this.loading = false;
					console.error('调用云函数失败:', error);
					uni.showToast({
						title: '网络错误，请检查连接后重试',
						icon: 'none'
					})
				}
			},
			goToLogin() {
				uni.redirectTo({
					url: '/pages/login/login'
				})
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		justify-content: center;
		align-items: center;
		min-height: 100vh;
		background-color: #f5f5f5;
		padding: 20rpx;
	}

	.form-container {
		background: white;
		padding: 60rpx 40rpx;
		border-radius: 16rpx;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.1);
		width: 100%;
		max-width: 600rpx;
	}

	.title {
		font-size: 48rpx;
		font-weight: bold;
		text-align: center;
		margin-bottom: 60rpx;
		display: block;
		color: #333;
	}

	.input-group {
		margin-bottom: 60rpx;
	}

	.input {
		border: 2rpx solid #e0e0e0;
		border-radius: 8rpx;
		padding: 24rpx;
		margin-bottom: 40rpx;
		font-size: 32rpx;
	}

	.input:focus {
		border-color: #007AFF;
	}

	.btn-primary {
		background-color: #007AFF;
		color: white;
		border: none;
		border-radius: 8rpx;
		padding: 24rpx;
		font-size: 32rpx;
		margin-bottom: 40rpx;
	}

	.btn-primary:disabled {
		background-color: #cccccc;
		color: #666666;
	}

	.link-text {
		text-align: center;
		font-size: 28rpx;
		color: #666;
	}

	.link {
		color: #007AFF;
		margin-left: 10rpx;
	}
</style>